package com.zc.study.thread.线程的基础使用;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SafeStopThread implements  Runnable{
    public static User user = new User();
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    private boolean stopMe = true;

    public void setStopMe(boolean stopMe) {
        this.stopMe = stopMe;
    }

    public boolean getStopMe() {
        return stopMe;
    }

    @Override
    public void run() {
        while (stopMe) {
            synchronized (SafeStopThread.class) {
                int v = (int) (System.currentTimeMillis() / 1000);
                user.setId(5);
                // to do sth
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                user.setName(String.valueOf(v));
                logger.debug("{}",user);
            }
            // 让出CPU，给其他线程执行
            Thread.yield();
        }

    }

    public static void main(String[] args) {
        Thread two = new Thread(new ThreadDemo2());
        two.start();
    }
}
